package com.example.seed.service;

import com.example.seed.dto.SysUserSpecification;
import com.example.seed.entity.*;
//import com.example.seed.fliter.DataScopeFilter;
import com.example.seed.utils.SpringSecurityUtil;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.spring.repository.SpringOrders;
import org.springframework.data.domain.Sort;

public interface SysUserRepository extends JRepository<SysUser, Long> {

    SysUserTable table = Tables.SYS_USER_TABLE;

    /**
     * 按帐户存在
     *
     * @param account 帐户
     * @return boolean
     */
    boolean existsByAccount(String account);

    /**
     * 按帐户查找
     *
     * @param account 帐户
     * @return {@link SysUser}
     */
    default SysUser findByAccount(String account) {
        return sql()
                //.filters(it -> it.disableByTypes(DataScopeFilter.class))
                .createQuery(table).where(table.account().eq(account)).select(table.fetch(Fetchers.SYS_USER_FETCHER.allTableFields().sysRoles(Fetchers.SYS_ROLE_FETCHER.allTableFields()))).fetchOne();
    }


    default Page<SysUser> page(int pageIndex, int pageSize, SysUserSpecification specification, SysUserFetcher fetcher, Sort sort) {
        return sql().createQuery(table).where(specification).orderBy(SpringOrders.toOrders(table, sort)).select(table.fetch(fetcher)).fetchPage(pageIndex, pageSize);
    }


    default SysUser currentLoginUser() {
        return SpringSecurityUtil.getLoginUser().getSysUser();
    }
}
